'use server'
import { get, post, patch, del } from "./fetch";

/**
 * 获取客户关联的预约列表
 * @param {string} clientId 客户ID
 * @returns {Promise<Array>} 预约列表
 */
export async function getClientAppointments(clientId, { branch_campus_id, court_id, status, start_time_from, start_time_to }) {
  try {
    const appointments = await get(`/api/v1/clients/${clientId}/appointments`, { branch_campus_id, court_id, status, start_time_from, start_time_to });
    return appointments;
  } catch (error) {
    throw error;
  }
}

/**
 * 获取教练关联的预约列表
 * @param {string} coachId 教练ID
 * @returns {Promise<Array>} 预约列表
 */
export async function getCoachAppointments(coachId, { status }) {
  try {
    const appointments = await get(`/api/v1/coaches/${coachId}/appointments`, { status });
    return appointments ;
  } catch (error) {
    throw error;
  }
}

/**
 * 获取教练关联的助教预约列表
 * @param {string} coachId 教练ID
 * @returns {Promise<Array>} 助教预约列表
 */
export async function getCoachAssistantAppointment(coachId, { branch_campus_id, court_id, status, start_time_from, start_time_to }) {
  try {
    const appointments = await get(`/api/v1/coaches/${coachId}/assistant-appointments`, { branch_campus_id, court_id, status, start_time_from, start_time_to });
    return appointments;
  } catch (error) {
    throw error;
  }
}

/**
 * 获取预约列表
 * @param {Object} params 查询参数
 * @returns {Promise<Array>} 预约列表
 */
export async function getAppointments({ coach_id,  branch_campus_id, court_id, status, start_time_from, start_time_to }) {
  try {
    const appointments = await get(`/api/v1/course-appointments`, { coach_id,  branch_campus_id, court_id, status, start_time_from, start_time_to });
    //查询每个课程预约的学生信息
    const appointmentsWithClients = await Promise.all(appointments.map(async (appointment) => {
      const clients = await get(`/api/v1/course-appointments/${appointment.id}/clients`);
      return { ...appointment, clients };
    }));
    return appointmentsWithClients;
  } catch (error) {
    throw error;
  }
}

/**
 * 创建课程预约
 * @param {Object} appointment 预约数据
 * @returns {Promise<Object>} 新预约
 */
export async function createCourseAppointment({sports_type_id, branch_campus_id, court_id, coach_id, start_time, end_time, teaching_commission_rate, memo, status}) {
  try {
    const newAppointment = await post(`/api/v1/course-appointments`, {sports_type_id, branch_campus_id, court_id, coach_id, start_time, end_time, teaching_commission_rate, memo, status});
    return newAppointment;
  } catch (error) {
    throw error;
  }
}

/**
 * 更新课程预约状态
 * @param {string} appointmentId 预约ID
 * @param {Object} appointment 预约数据
 * @returns {Promise<Object>} 更新后的预约
 */
export async function updateCourseAppointmentStatus(appointmentId, appointment) {
  try {
    const updatedAppointment = await patch(`/api/v1/course-appointments/${appointmentId}/status`, appointment);
    return updatedAppointment;
  } catch (error) {
    throw error;
  }
}


/**
 * 获取课程预约的客户列表
 * @param {string} appointmentId 预约ID
 * @returns {Promise<Array>} 客户列表
 */
export async function getCourseAppointmentClients(appointmentId) {
  try {
    const clients = await get(`/api/v1/course-appointments/${appointmentId}/clients`);
    return clients;
  } catch (error) {
    throw error;
  }
}

/**
 * 添加课程预约客户
 * @param {string} appointmentId 预约ID
 * @param {Object} client 客户数据
 * @returns {Promise<Object>} 新客户
 */
export async function addCourseAppointmentClient(appointmentId, {client_id, memo}) {
  try {
    const client = await post(`/api/v1/course-appointments/${appointmentId}/clients`, { client_id, memo });
    return client;
  } catch (error) {
    throw error;
  }
}

/**
 * 移除课程预约客户
 * @param {string} appointmentId 预约ID
 * @param {string} clientId 客户ID
 */
export async function removeCourseAppointmentClient(appointmentId, clientId) {
  try {
    await del(`/api/v1/course-appointments/${appointmentId}/clients`, { client_id: clientId });
  } catch (error) {
    throw error;
  }
}

/**
 * 获取课程预约的套餐列表
 * @param {string} appointmentId 预约ID
 * @returns {Promise<Array>} 套餐列表
 */
export async function getCourseAppointmentPackages(appointmentId) {
  try {
    const packages = await get(`/api/v1/course-appointments/${appointmentId}/packages`);
    return packages;
  } catch (error) {
    throw error;
  }
}


/**
 * 添加课程预约套餐
 * @param {string} appointmentId 预约ID
 * @param {Object} appointmentPackage 套餐数据
 * @returns {Promise<Object>} 新套餐
 */
export async function addCourseAppointmentPackage(appointmentId, {course_package_id, status, memo}) {
  try {
    const appointmentPackage = await post(`/api/v1/course-appointments/${appointmentId}/packages`, {course_package_id, status, memo});
    return appointmentPackage;
  } catch (error) {
    throw error;
  }
}

/**
 * 移除课程预约套餐
 * @param {string} appointmentId 预约ID
 * @param {string} packageId 套餐ID
 */
export async function removeCourseAppointmentPackage(appointmentId, packageId) {
  try {
    await del(`/api/v1/course-appointments/${appointmentId}/packages/${packageId}`);
  } catch (error) {
    throw error;
  }
}

/**
 * 获取课程预约的助教列表
 * @param {string} appointmentId 预约ID
 * @returns {Promise<Array>} 助教列表
 */
export async function getCourseAppointmentAssistantCoaches(appointmentId) {
  try {
    const assistantCoaches = await get(`/api/v1/course-appointments/${appointmentId}/assistant-coaches`);
    return assistantCoaches;
  } catch (error) {
    throw error;
  }
}

/**
 * 添加课程预约助教
 * @param {string} appointmentId 预约ID
 * @param {Object} assistantCoach 助教数据
 * @returns {Promise<Object>} 新助教
 */
export async function addCourseAppointmentAssistantCoach(appointmentId, {coach_id, teaching_commission_rate, status, memo}) {
  try {
    const assistantCoach = await post(`/api/v1/course-appointments/${appointmentId}/assistant-coaches`, {coach_id, teaching_commission_rate, status, memo});
    return assistantCoach;
  } catch (error) {
    throw error;
  }
}

/**
 * 更新课程预约助教
 * @param {string} appointmentId 预约ID
 * @param {string} coachId 助教ID
 * @param {Object} assistantCoach 助教数据
 * @returns {Promise<Object>} 更新后的助教
 */
export async function updateCourseAppointmentAssistantCoach(appointmentId, coachId, {teaching_commission_rate, status, memo}) {
  try {
    const assistantCoach = await patch(`/api/v1/course-appointments/${appointmentId}/assistant-coaches/${coachId}`, {teaching_commission_rate, status, memo});
    return assistantCoach;
  } catch (error) {
    throw error;
  }
}

/**
 * 移除课程预约助教
 * @param {string} appointmentId 预约ID
 * @param {string} coachId 助教ID
 */
export async function removeCourseAppointmentAssistantCoach(appointmentId, coachId) {
  try {
    await del(`/api/v1/course-appointments/${appointmentId}/assistant-coaches/${coachId}`);
  } catch (error) {
    throw error;
  }
}


/**
 * 获取课程预约的计划列表
 * @param {string} appointmentId 预约ID
 * @returns {Promise<Array>} 计划列表
 */
export async function getCourseAppointmentPlans(appointmentId) {
  try {
    const plans = await get(`/api/v1/course-appointments/${appointmentId}/plans`);
    return plans;
  } catch (error) {
    throw error;
  }
}

/**
 * 添加课程预约计划
 * @param {string} appointmentId 预约ID
 * @param {Object} plan 计划数据
 * @returns {Promise<Object>} 新计划
 */
export async function addCourseAppointmentPlan(appointmentId, {course_plan_id, status, memo}) {
  try {
    const plan = await post(`/api/v1/course-appointments/${appointmentId}/plans`, {course_plan_id, status, memo});
    return plan;
  } catch (error) {
    throw error;
  }
}

/**
 * 更新课程预约计划
 * @param {string} appointmentId 预约ID
 * @param {string} planId 计划ID
 * @param {Object} plan 计划数据
 * @returns {Promise<Object>} 更新后的计划
 */
export async function updateCourseAppointmentPlan(appointmentId, planId, {status, memo}) {
  try {
    const plan = await patch(`/api/v1/course-appointments/${appointmentId}/plans/${planId}`, {status, memo});
    return plan;
  } catch (error) {
    throw error;
  }
}

/**
 * 移除课程预约计划
 * @param {string} appointmentId 预约ID
 * @param {string} planId 计划ID
 */
export async function removeCourseAppointmentPlan(appointmentId, planId) {
  try {
    await del(`/api/v1/course-appointments/${appointmentId}/plans/${planId}`);
  } catch (error) {
    throw error;
  }
}